#include "compile_run.hpp"
#include "../common/httplib.h"

using namespace yx;
// using namespace httplib;

int main(int argc, char **argv)
{
    if (argc != 2)
    {
        std::cout << argv[0] << " + port" << std::endl;

        return -1;
    }

    httplib::Server svr;
    auto func = [](const httplib::Request &req, httplib::Response &resp)
    {
        std::string in_json = req.body;
        std::string out_json;

        if (!in_json.empty())
        {
            CompileRun::start(in_json, out_json);
            resp.set_content(out_json, "application/json;charset=utf-8");
        }
    };
    svr.Post("/compile_run", func);

    svr.listen("0.0.0.0", std::atoi(argv[1]));

    return 0;
}

// void test_CompileRun()
// {
//     Json::Value in_value;
//     in_value["code"] = R "(#include <iostream>
//         int
//         main()
//     {
//         int *p = new int[1111111111];
//         while (true)
//             ;
//         std::cout << "hello CompileAndRun" << std::endl;
//         return -1;
//     })";
//     in_value["input"] = "";
//     in_value["cpu_limit"] = 1;
//     in_value["mem_limit"] = 5 * 10240;

//     Json::FastWriter writer;
//     std::string in_json = writer.write(in_value);

//     std::string out_json;
//     CompileRun::start(in_json, out_json);

//     std::cout << out_json << std::endl;
// }
